// pages/case/case.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shortVideo:[],//短视频
    list: [],
    userName: '',
    mobile: '',
    indexCurrent:null,
    height:''//页面高度
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      var shortVideo = this.data.shortVideo
      var height = this.data.height
      // 初始化页面
      this.setData({
        currentTab: 0,
      })
      // 网络请求
      wx.request({
        url: 'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/json/caseShortVideo.json',
        method:'GET',
        header:{
          'content-type': 'application/json'
        },
        success: function (res){
          that.setData({
            shortVideo : res.data
          })
          var arr = Object.keys(res.data)//获取对象的长度
          var pageHeight=arr.length-2
          that.setData({
            height : pageHeight*513//swipe高度
          })
        }, fail: function (failSuccess){
          //console.log(failSuccess)//测试
          wx.showModal({
            content: '网络开了小差，是否刷新？',
            success(res) {
              if (res.confirm) {
                //页面刷新
                const pages = getCurrentPages()
                const perpage = pages[pages.length - 1]
                perpage.onLoad()
              } else if (res.cancel) {
                
              }
            }
          })
        }
      })
  },
  // 滑块切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
      this.request()
    }
  },
  //点击切换
  swiperChange: function (e) {
    this.setData({
      currentTab: e.detail.current,
    })
  },

  request:function(res){
    var mum = this.data.currentTab
    var that = this
    if (mum == 1) {
      var Url = 'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/json/publicServiceAds.json'//公益广告
   } else if (mum == 2) {
      var Url = 'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/json/vlog.json'//vlog
   } else if (mum == 3) {
      var Url = 'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/json/microFilm.json'//microFilm
   } else if (mum == 4) {
     //var Url = 'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/json/publicServiceAds.json'//公益广告
   }
  wx.request({
    url: Url,
    method:'GET',
    header:{
      'content-type': 'application/json'
    },
    success:function(res){
      that.setData({
        shortVideo : res.data
      })
      var arr = Object.keys(res.data)//获取对象的长度
      var pageHeight=arr.length-2
      that.setData({
        height : pageHeight*513//swipe高度
      })
    },fail:function(res){
      console.log(res)
    }
   })
  },
  //播放控制
  video_play:function(e){
    var that = this;
    var curIdx = e.currentTarget.dataset.index;
    console.log(curIdx)
    if (that.data.indexCurrent != null) {
      var videoContextPrev = wx.createVideoContext('myVideo' + that.data.indexCurrent)
      if (that.data.indexCurrent != curIdx) {
        videoContextPrev.pause()
      }
      that.setData({
        indexCurrent: curIdx
      })
      var videoContextCurrent = wx.createVideoContext('myVideo' + curIdx)
      videoContextCurrent.play()
    }else{
      that.setData({
        indexCurrent: curIdx
      })
      var videoContext = wx.createVideoContext('myVideo' + curIdx) //这里对应的视频id
      videoContext.play()
    }
  },
  play(e) {
    var that = this;
    var id = e.currentTarget.id;
    for (var i = 0; i < that.data.healthKjList.length; i++) {
      if (id === 'myVideo' + i) {
        //console.log('播放视频不做处理');
      } else {
        //console.log('暂停其他正在播放的视频');
        var videoContext = wx.createVideoContext("myVideo"+i, that);
        videoContext.pause();
      }
    }
  },

  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },

  mobileInput: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  btnclick: function () {
    var userName = this.data.userName;
    var mobile = this.data.mobile;
    var phonetel = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var name = /^[u4E00-u9FA5]+$/;
    
    if (userName == '') {
      wx.showToast({
        title: '请输入名字',
        icon: 'succes',
        duration: 1000,
        mask: true
      })

      return false
    } else if (mobile == '') {
      wx.showToast({
        title: '手机号不能为空',
      })

      return false
    }
    else if (mobile.length != 11) {
      wx.showToast({
        title: '号码长度有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    return true;
  },
})